<?php

/**
 * Class Manage_con   酒店管理
 */
class Manage_con extends Lxbase{

    /**
     * 添加楼层
     */
    public function addFloor(){
        $data = array();
        $data['uid'] = $_SESSION['userinfo']['userid'];
        $data['hid'] = $_SESSION['hyid'];
        $data['num'] = addslashes($_POST['num']);
        $data['remark'] = addslashes($_POST['remark']);
        $res = $this->setdata('hotel_floor',$data);
        header('location:?m=manage&a=getRoomsByFtId');exit;
    }

    /**
     * 删除楼层
     */
    public function delFloor(){
        $fid = $_REQUEST['fid'];
        // 判断此楼层下是否有房间
        $res = $this->getFloors($fid);
        if(!$res){
            $this->deldata('hotel_floor',' where id = ' . $fid);
            echo json_encode(array('stat'=>1,'msg'=>'删除成功'));exit;
        }else{
            echo json_encode(array('stat'=>0,'msg'=>'删除失败,此楼层下还有房间'));exit;
        }
    }

    /**
     * 根据楼层id和酒店id，取出房间
     */
    public function getFloors($fid){
        $hid = $_SESSION['hyid'];
        //$fid = $_REQUEST['fid'];
        $where = " where hid = " . $hid . " and fid = " . $fid;
        $res = $this->arrdata('*','hotel_room',$where);
        return $res;
    }

    /**
     * 添加房型
     */
    public function addHotelType(){
        $data = array();
        $data['uid'] = $_SESSION['userinfo']['userid'];
        $data['hid'] = $_SESSION['hyid'];
        $data['name'] = addslashes($_POST['name']);
        $data['area'] = $_POST['tarea'];
        $data['pic'] = $_POST['logourl'];
        $data['remark'] = addslashes($_POST['remark']);
        $res = $this->setdata('hotel_type',$data);
        if($_POST['linktype']==1){
            $func = 'getRoomsByFtId';
        }else{
            $func = 'getRoomsByTid';
        }
        header('location:?m=manage&a=' . $func);exit;
    }

    /**
     * 删除房型
     */
    public function delHotelType(){
        $tid = $_REQUEST['tid'];
        //判断此房型下是否有房间
        $res = $this->getRooms($tid);
        if(!$res){
            $this->deldata('hotel_type',' where id = ' . $tid);
            echo json_encode(array('stat'=>1,'msg'=>'删除成功'));exit;
        }else{
            echo json_encode(array('stat'=>0,'msg'=>'删除失败,此房型下还有房间'));exit;
        }
    }

    /**
     * 根据房型id和酒店id，取出房间
     */
    public function getRooms($tid){
        $hid = $_SESSION['hyid'];
        //$tid = $_REQUEST['tid'];
        $where = " where hid = " . $hid . " and tid = " . $tid;
        $res = $this->arrdata('*','hotel_room',$where);
        return $res;
    }

    /**
     * 根据酒店id取出楼层
     */
    public function getFloorByHid($hid=0){
        return $this->arrdata('*','hotel_floor',' where hid = ' . (int)$hid);
    }

    /**
     * 根据酒店id取出房型
     */
    public function getTypesByHid($hid=0){
        return $this->arrdata('*','hotel_type',' where hid = ' . (int)$hid);
    }

    /**
     * 根据酒店id获取设施
     */
    public function getFacByHid($hid=0){
        return $this->arrdata('*','hotel_facilities',' where hid = 0 or hid = ' . $hid);
    }

    /**
     * 添加酒店设施
     */
    public function addFac(){
        $data = array();
        $data['name'] = $_POST['facname'];
        $data['hid'] = $_SESSION['hyid'];
        $res = $this->setdata('hotel_facilities',$data);
        echo json_encode(array('stat'=>1,'msg'=>$res));
    }

    /**
     * 删除房间
     */
    public function delRoom(){
        $rid = $_REQUEST['rid'];
        $this->deldata('hotel_room',' where id = ' . (int)$rid);
        echo '删除成功';exit;
    }

    /**
     *  根据房型id获取名字
     */
    public function getTnameByid($id=0){
        return $this->viewdata('name','hotel_type','where id = ' . $id);
    }

    /**
     * 添加房间
     */
    public function addRoom(){
        $data = array();
        $data['tel'] = addslashes($_POST['tel']);
        $data['fid'] = $_POST['fid'];
        $data['tid'] = $_POST['tid'];
        $data['pid'] = $_POST['pid'];
        $data['name'] = str_replace('，',',',addslashes($_POST['name']));
        $data['status'] = $_POST['status'];
        $data['remark'] = addslashes($_POST['remark']);
        //处理房间设施数据
        $data['fac_list'] = '';
        foreach($_POST['fac_list'] as $g=>$h){
            $data['fac_list'] .= $h . ',';
        }
        $data['fac_list'] = rtrim($data['fac_list'],',');
        $data['uid'] = $_SESSION['userinfo']['userid'];
        $data['hid'] = $_SESSION['hyid'];
        $data['rtype'] = 1;
        if(strstr($data['name'],',')!==false){
            $tempstr = explode(',',$data['name']);
            foreach($tempstr as $m=>$n){
                $data['name'] = $n;
                $this->setdata('hotel_room',$data);
            }
        }else{
            $res = $this->setdata('hotel_room',$data);
        }
        if($_POST['linktype']==1){
            $func = 'getRoomsByFtId';
        }else{
            $func = 'getRoomsByTid';
        }
        header('location:?m=manage&a=' . $func);exit;
    }

    /**
     * 添加房型方案
     */
    public function addTypePro(){
        $tid = $_REQUEST['tid'];
        //判断此房型的方案是否已经超过5个
        $pnum = $this->numdata('hotel_type_project',' where hid = ' . $_SESSION['hyid'] . ' and tid = ' . $tid);
        if($pnum==5){
            echo '此房型的方案已经超过5个，不能添加';exit;
        }
        $data = array();
        $data['tid'] = $tid;
        $data['hid'] = $_SESSION['hyid'];
        $data['name'] = addslashes($_POST['name']);
        $data['pay_type'] = $_POST['pay_type'];
        $data['is_breakfast'] = $_POST['is_breakfast'];
        $data['status'] = '2';  //默认关闭状态
        $data['pacific_price'] = $_POST['pacific_price'];
        $data['weekend_price'] = $_POST['weekend_price'];
        $data['special_price'] = $_POST['special_price'];
        //预约方式为多选，需处理
        $data['subscribe_type'] = $_POST['subscribe_type'];
        $res = $this->setdata('hotel_type_project',$data);
        if($res){
            echo '添加成功';exit;
        }else{
            echo '添加失败';exit;
        }
    }

    /**
     * 根据房型id和酒店id取出方案
     */
    public function getProByTid($tid=0,$hid=0){
        return $this->arrdata('*','hotel_type_project',' where hid = ' . (int)$hid . ' and tid = ' . (int)$tid);
    }

    /**
     * 根据方案id取出详细信息
     */
    public function getPinfoById(){
        $pid = $_REQUEST['id'];
        return $this->viewdata('*','hotel_type_project',' where id = ' . (int)$pid);
    }

    /**
     * 根据方案id取出详细信息  AJax
     */
    public function getPinfoByIdAjax(){
        $pid = $_GET['id'];
        $res = $this->viewdata('*','hotel_type_project',' where id = ' . (int)$pid);
        echo json_encode(array('stat'=>1,'msg'=>$res));exit;
    }

    /**
     * 添加方案信息
     */
    public function addProInfo(){
        $data = array();
        $data['hid'] = $_SESSION['hyid'];
        $data['tid'] = $_POST['tid'];
        $data['name'] = addslashes($_POST['name']);
        $data['pay_type'] = $_POST['pay_type'];
        $data['is_breakfast'] = $_POST['is_breakfast'];
        $data['pacific_price'] = $_POST['pacific_price'];
        $data['weekend_price'] = $_POST['weekend_price'];
        $data['special_price'] = $_POST['special_price'];
        $data['status'] = $_POST['status'];
        $this->setdata('hotel_type_project',$data);
        header('location:?m=manage&a=setPro');exit;
    }

    /**
     * 设置方案信息
     */
    public function setProInfo(){
        $data = array();
        $data['name'] = addslashes($_POST['name']);
        $data['pay_type'] = $_POST['pay_type'];
        $data['is_breakfast'] = $_POST['is_breakfast'];
        $data['pacific_price'] = $_POST['pacific_price'];
        $data['weekend_price'] = $_POST['weekend_price'];
        $data['special_price'] = $_POST['special_price'];
        $data['status'] = $_POST['status'];
        $this->updata('hotel_type_project',$data,' where id = ' . (int)$_POST['pid']);
        header('location:?m=manage&a=setPro');exit;
    }

    /**
     * 设置一个房型的方案状态（开启/关闭）
     */
    public function setProStat(){
        $pid = $_REQUEST['pid'];
        $status = $_REQUEST['status'];
        $res = $this->updata('hotel_type_project','status="' . $status . '"',' where id = ' . (int)$pid);
        if($res){
            echo json_encode(array('stat'=>1,'msg'=>'设置成功'));exit;
        }else{
            echo json_encode(array('stat'=>0,'msg'=>'设置失败'));exit;
        }
    }

    /**
     * 设置酒店的特殊日
     */
    public function setTimes(){
        //特殊日为多个时间拼接成一个字符串，需处理
        $data = array();
        $data['times'] = '';
        $data['hid'] = $_SESSION['hyid'];
        $res = $this->setdata('hotel_times',$data);
        if($res){
            echo '设置成功';exit;
        }else{
            echo '设置失败';exit;
        }
    }

    /**
     * 根据楼层、房型筛选房间
     */
    public function getRoomsByFtId(){
        $_where = ' where uid = ' . $_SESSION['userinfo']['userid'];
        $pam = array();
        if(isset($_REQUEST['tid'])){
            $pam['tid'] = $_REQUEST['tid'];
        }
        if(isset($_REQUEST['fid'])){
            $pam['fid'] = $_REQUEST['fid'];
        }
        if(isset($_REQUEST['fid']) && $_REQUEST['fid']){
            $_where .= ' and fid = ' . $_REQUEST['fid'];
        }
        if(isset($_REQUEST['tid']) && $_REQUEST['tid']){
            $_where .= ' and tid = ' . $_REQUEST['tid'];
        }
        $rooms = $this->arrdata('*','hotel_room',$_where);
        foreach($rooms as $m=>$n){
            $rooms[$m]['tname'] = $this->getTnameByid($n['tid'])['name'];
        }
        $this->smarty->assign('pam',$pam);
        $this->smarty->assign('rooms',$rooms);
        $this->smarty->assign('facs',$this->getFacByHid($_SESSION['hyid']));
        $this->smarty->assign('floors',$this->getFloorByHid($_SESSION['hyid']));
        $this->smarty->assign('types',$this->getTypesByHid($_SESSION['hyid']));
        $this->smarty->display('room_management.tpl');
    }

    /**
     * 根据房型筛选房间
     */
    public function getRoomsByTid(){
        $_where = ' where uid = ' . $_SESSION['userinfo']['userid'];
        $pam = array();
        if(isset($_REQUEST['tid'])){
            $pam['tid'] = $_REQUEST['tid'];
        }
        if(isset($_REQUEST['tid']) && $_REQUEST['tid']){
            $_where .= ' and tid = ' . $_REQUEST['tid'];
        }
        $rooms = $this->arrdata('*','hotel_room',$_where);
        foreach($rooms as $m=>$n){
            $rooms[$m]['tname'] = $this->getTnameByid($n['tid'])['name'];
        }
        $this->smarty->assign('rooms',$rooms);
        $this->smarty->assign('pam',$pam);
        $this->smarty->assign('facs',$this->getFacByHid($_SESSION['hyid']));
        $this->smarty->assign('floors',$this->getFloorByHid($_SESSION['hyid']));
        $this->smarty->assign('types',$this->getTypesByHid($_SESSION['hyid']));
        $this->smarty->display('room_management_2.tpl');
    }

    /**
     * 房态筛选
     */
    public function index(){
        $_where = ' where status = 1 and uid = ' . $_SESSION['userinfo']['userid'];
        $pam = array();
        //房型
        if(isset($_REQUEST['tid']) && $_REQUEST['tid']){
            $_where .= ' and tid = ' . $_REQUEST['tid'];
            $pam['tid'] = $_REQUEST['tid'];
        }
        //房态
        if(isset($_REQUEST['rtype']) && $_REQUEST['rtype']){
            $_where .= ' and rtype = ' . $_REQUEST['rtype'];
            $pam['rtype'] = $_REQUEST['rtype'];
        }
        //楼层
        if(isset($_REQUEST['fid']) && $_REQUEST['fid']){
            $_where .= ' and fid = ' . $_REQUEST['fid'];
            $pam['fid'] = $_REQUEST['fid'];
        }
        //房间特征
        if(isset($_REQUEST['facid']) && $_REQUEST['facid']){
            $_where .= ' and find_in_set(' . $_REQUEST['facid'] . ',fac_list)';
            $pam['facid'] = $_REQUEST['facid'];
        }
        //房号
        if(isset($_REQUEST['rname']) && $_REQUEST['rname']){
            $_where .= ' and name = "' . $_REQUEST['rname'] . '"';
            $pam['rname'] = $_REQUEST['rname'];
        }
        $rooms = $this->arrdata('*','hotel_room',$_where);
        foreach($rooms as $m=>$n){
            $rooms[$m]['tname'] = $this->getTnameByid($n['tid'])['name'];
        }
        $this->smarty->assign('rooms',$rooms);
        // 此酒店下所有房型
        $this->smarty->assign('types',$this->getTypesByHid($_SESSION['hyid']));
        // 此酒店下所有房态
        $this->smarty->assign('rtypes',array('1'=>'空房','2'=>'在住','3'=>'预订','4'=>'打扫'));
        // 不同状态不同颜色
        $this->smarty->assign('colors',array('1'=>'blue','2'=>'red','3'=>'green','4'=>'gray'));
        // 此酒店下所有楼层
        $this->smarty->assign('floors',$this->getFloorByHid($_SESSION['hyid']));
        // 此酒店下所有设施
        $this->smarty->assign('facs',$this->getFacByHid($_SESSION['hyid']));
        // 计算出各种房态的房间数量
        $res = $this->arrdata('rtype,count(*) as nums','hotel_room','where status = 1 and hid = '.$_SESSION['hyid'].' group by rtype');  // 处理$res，将房态编号设为key，房间数量设为val
        $temp = array();
        foreach($res as $a=>$b){
            $temp[$b['rtype']]['nums'] = $b['nums'];
        }
        $this->smarty->assign('nums',$temp);
        // 计算出此酒店所有房间数量
        $rnum = $this->numdata('hotel_room','where status = 1 and hid = ' . $_SESSION['hyid']);
        $this->smarty->assign('pam',$pam);
        $this->smarty->assign('rnum',$rnum);
        $this->smarty->assign('pay_type',$this->getconfig('hotel')['pay_type']); //支付方式
        $this->smarty->display('room_storey.tpl');
    }

    /**
     * 酒店基础设置
     */
    public function setBaseInfo(){
        if($_POST){
            $data = array();
            $uid= $_SESSION['userinfo']['userid'];
            $name = $_POST['name'];
            $ename = $_POST['ename'];
            $person = $_POST['person'];
            $tel = str_replace(' ','',$_POST['tel']);
            $phone = str_replace(' ','',$_POST['phone']);
            $province = $_POST['province'];
            $city= $_POST['city'];
            $district = $_POST['district'];
            $address = $_POST['address'];
            $latitude= $_POST['latitude'];
            $longitude = $_POST['longitude'];
			$bdgps = $this->getbdgps($longitude,$latitude,3);
			if(!empty($bdgps)){
				$latitude = $bdgps["y"];
				$longitude = $bdgps["x"];
			}
			if(!empty($_POST["pic"])){
				$pic=serialize($_POST["pic"]);
			}
			$logourl = $_POST["logourl"];
            $intro1 = $_POST['intro1'];
			$intro2 = $_POST['intro2'];
			$intro3 = $_POST['intro3'];
			$hid=empty($_POST["hid"])?$_SESSION["hyid"]:(int)$_POST["hid"];
			$into = "uid={$uid},name='{$name}',ename='{$ename}',person='{$person}',tel='{$tel}',phone='{$phone}',province='{$province}',city='{$city}',logo='{$logourl}'";
			$into.=",district='{$district}',address='{$address}',latitude={$latitude},longitude={$longitude},pic='{$pic}',intro1='{$intro1}',intro2='{$intro2}',intro3='{$intro3}'";
            if(!empty($hid)){
                //编辑
                $res = $this->updata('hotel',$into,"where id={$hid}");
            }else{
                //添加
                $res = $this->setdata('hotel',$into);
                $_SESSION['hyid'] = $res;
            }
            if($res){
                echo "<script>alert('操作成功');top.showhotel();parent.location.href='?m=manage&a=setBaseInfo&'+Math.random();</script>";exit;
            }
        }
        $res = $this->viewdata('*','hotel','where uid='.$_SESSION["userinfo"]["userid"]);
		$res["pic"] = unserialize($res["pic"]);
		$res["url"] = $this->basehost."minisite/hotel/?hid=".$res["id"];
		$res["rcode"] = $this->getrcode($res["url"]);
        if($res){
            $this->smarty->assign('info',$res);
        }else{
            $this->smarty->assign('info',array());
        }
		$this->smarty->assign("baseurl",$this->basehost);
        $this->smarty->display("hotel_set_up.tpl");
    }

    /**
     * 设置方案
     */
    public function setPro(){
        $hid = $_SESSION['hyid'];
        $tres = $this->getTypesByHid($hid);
        foreach($tres as $k=>$v){
            $tres[$k]['pinfo'] = $this->getProByTid($v['id'],$hid);
            $tres[$k]['pro_num'] = count($tres[$k]['pinfo']);
        }
        $this->smarty->assign('tres',$tres);
        $this->smarty->display("room_management_3.tpl");
    }

    /**
     * ajax 图片上传
     */
    public function uploadimg(){
        if($_FILES['uploadimg']['error']){
            echo json_encode(array('status'=>0,'msg'=>'上传失败'));exit;
        }
        $uptypes = array(
            'image/jpg',
            'image/jpeg',
            'image/png',
            'image/pjpeg',
            'image/gif',
            'image/bmp',
            'image/x-png'
        );
        if(!in_array($_FILES['uploadimg']['type'],$uptypes)){
            echo json_encode(array('status'=>0,'msg'=>'上传失败,文件类型不正确'));exit;
        }
        if($_FILES['uploadimg']['size']>2097152){
            echo json_encode(array('status'=>0,'msg'=>'上传失败,文件大小不能超过2M'));exit;
        }
        $res = $this->upload($_FILES['uploadimg'],'user');
        echo json_encode(array('status'=>1,'msg'=>$res['pic']));exit;
    }

    /**
     * 删除房间设施
     */
    public function delfac(){
        $id = $_GET['id'];
        $this->deldata('hotel_facilities','where id = ' . $id);
        echo '删除成功';exit;
    }

    /**
     * 根据房间Id 取出 方案信息
     */
    public function getProInfo(){
        $room_id = $_GET['id'];
        //根据房间id 取出 方案id
        $pid = $this->viewdata('pid','hotel_room','where id = '.(int)$room_id)['pid'];
        //根据方案id 取出详细信息
        $res = $this->viewdata('id,name,pacific_price,weekend_price,special_price','hotel_type_project','where id = '.(int)$pid);
        echo json_encode($res);exit;
    }

}